package com.cskaoyan.servlet;

import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.service.SystemConfigService;
import com.cskaoyan.service.SystemConfigServiceImpl;
import com.cskaoyan.util.JacksonUtil;
import com.cskaoyan.util.ResponseUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @author Ramsey
 * @date 2023年05月30日 11:31
 */
@WebServlet("/admin/config/*")
public class AdminConfigServlet extends WdBaseServlet {
    private SystemConfigService systemConfigService = new SystemConfigServiceImpl();

    private void mall(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String method = request.getMethod();
        if ("GET".equals(method)) {
            //GET方法做查询
            System.out.println("-------------------查询---------------------");
            mallQuery(request, response);
        } else {
            //POST方法做更新
            System.out.println("-------------------更新-----------------------");
            mallModify(request, response);
        }
    }

    private void mallModify(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String jsonStr = request.getReader().readLine();
        Map<String, String> data = JacksonUtil.read(jsonStr, Map.class);
        systemConfigService.updateConfig(data);
        ResponseUtil.responseOkJsonVo(response, null);
    }

    private void mallQuery(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String, String> data = systemConfigService.listMall();
        ResponseUtil.responseOkJsonVo(response, data);
    }

    //新增的yongijan的
    private BaseRespVo express(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获得请求报文的请求方法
        String method = request.getMethod();

        // 判断请求方法是什么
        if ("GET".equals(method)) {
            // get类型
            return systemConfigService.expressGet();
        } else {
            // Post类型
            // 获得JSON字符串
            String jsonStr = request.getReader().readLine();
            // 封装JSON字符串
            Map<String, Object> map = JacksonUtil.read(jsonStr, Map.class);
            return systemConfigService.configPost(map);
        }
    }


    //
    private BaseRespVo order(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获得请求报文的请求方法
        String method = request.getMethod();

        // 判断请求方法是什么
        if ("GET".equals(method)) {
            // get类型
            return systemConfigService.orderGet();
        } else {
            // Post类型
            // 获得JSON字符串
            String jsonStr = request.getReader().readLine();
            // 封装JSON字符串
            Map<String, Object> map = JacksonUtil.read(jsonStr, Map.class);
            return systemConfigService.configPost(map);
        }
    }

    //
    private BaseRespVo wx(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获得请求报文的请求方法
        String method = request.getMethod();

        // 判断请求方法是什么
        if ("GET".equals(method)) {
            // get类型
            return systemConfigService.wxGet();
        } else {
            // Post类型
            // 获得JSON字符串
            String jsonStr = request.getReader().readLine();
            // 封装JSON字符串
            Map<String, Object> map = JacksonUtil.read(jsonStr, Map.class);
            return systemConfigService.configPost(map);
        }
    }

}
